#include "CustomerInputDialog.h"
#include "MainWindow.h"
#include "Shop.h"

#include <QMessageBox>

using namespace ecocity;

static const QString all_customers = "Todos";

//---------------------------------------------------------------

CustomerInputDialog::CustomerInputDialog( const std::vector< Customer >& customers,
                                          QWidget *parent )
    : QDialog( parent ),
      m_customers_model( customers ),
      customers_filter_model( new QSortFilterProxyModel )
{
    setupUi( this );

    setWindowTitle( "Selecciona el Cliente" );

    // Set up Models
    customers_filter_model->setSourceModel( &m_customers_model );
    customers_filter_model->setFilterKeyColumn( CustomersModel::CUSTOMER_PRICE_TYPE );

    // Set up views
    customers_view->setModel( customers_filter_model );

    // Set up ComboBox
    QStringList customer_types;
    customer_types << all_customers;
    customer_types.append( Shop::customer_types() );

    for ( int i = 0; i < customer_types.size(); ++i )
    {
        customer_types_combo->addItem( customer_types.at( i ) );
    }

    customers_view->resizeColumnsToContents( );
    customers_view->horizontalHeader()->setStretchLastSection( true );

    // Customer Type ComboBox changedIndex
    connect( customer_types_combo, SIGNAL( currentIndexChanged( QString ) ),
             this, SLOT( show_customer_type( QString ) ) );
}

//---------------------------------------------------------------

QString
CustomerInputDialog::get_customer_type( ) const
{
    return customer_types_combo->currentText( );
}

//---------------------------------------------------------------

QString
CustomerInputDialog::get_customer_name( ) const
{
    return m_customer_name;
}

//---------------------------------------------------------------

void
CustomerInputDialog::show_customer_type( QString type )
{
    customers_filter_model->setFilterKeyColumn( CustomersModel::CUSTOMER_PRICE_TYPE );

    if( type == all_customers )
    {
        type = "";
    }

    customers_filter_model->setFilterRegExp( type );
}

//---------------------------------------------------------------

void
CustomerInputDialog::accept( )
{
    QItemSelectionModel* model = customers_view->selectionModel( );

    if ( !model->hasSelection( ) )
    {
        QMessageBox::warning( this, "Customer not selected", "Customer not selected" );

        return;
    }

    QModelIndex proxy_index = customers_view->currentIndex( );

    if ( !proxy_index.isValid( ) )
    {
        QMessageBox::warning( this, "Customer Index found", "Customer Index not found" );
        return;
    }

    // Use id in general
    //int id = customers_filter_model->data(
    //            customers_filter_model->index( index.row( ),
    //                                     ProductsModel::PRODUCT_ID ) ).toInt( );

    m_customer_name = customers_filter_model->data(
                customers_filter_model->index( proxy_index.row( ),
                                         CustomersModel::CUSTOMER_NAME ) ).toString( );

    // Call base class in order to close dialog
    QDialog::accept();
}
